<?php

namespace app\admin\controller;
use app\admin\model\HouseModel;
use think\Db;

class House extends Base{

    /**
     * 题库列表
     */
    public function index(){
        $key = input('key');
        $map = [];
        $map['status'] = array('eq',1);
        if($key&&$key!=="")
        {
            $map['title'] = ['like',"%" . $key . "%"];
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $count = Db('house')->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $house = new HouseModel();
        $lists = $house->getAll($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('val', $key);
        if(input('get.page'))
        {
            return json($lists);
        }
        return $this->fetch();
    }

    /**
     * 添加
     */
    public function add(){
        if(request()->isAjax()){
            $param = input('post.');
            $house = new HouseModel();
            $flag = $house->insertHouse($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $position = array(array('id'=>'A','name'=>'A'),array('id'=>'B','name'=>'B'),array('id'=>'C','name'=>'C'),array('id'=>'D','name'=>'D'));
        $this->assign('answer',$position);
        return $this->fetch();
    }


    public function edit(){
        if(request()->isAjax()){
            $param = input('post.');
            $house = new HouseModel();
            $flag = $house->updateHouse($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $position = array(array('id'=>'A','name'=>'A'),array('id'=>'B','name'=>'B'),array('id'=>'C','name'=>'C'),array('id'=>'D','name'=>'D'));
        $house  = Db::name('house')->where('id','eq',input('id'))->find();
        $result = json_decode($house['result'],true);
        unset($house['result']);
        $house['A'] = $result['A'];
        $house['B'] = $result['B'];
        $house['C'] = $result['C'];
        $house['D'] = $result['D'];
        $this->assign('answer',$position);
        $this->assign('house',$house);
        return $this->fetch();
    }
}